﻿// --------------------------------
// <copyright file="BackgroundItem.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>7. 12. 2011 21:04:25</date>

// <summary>
//
// </summary>
// --------------------------------

using System;
using FITClub.Episode1.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FITClub.Episode1.Levels
{
    public class BackgroundItem : DrawableGameComponent, IConfigComponent
    {
        private Matrix worldMatrix;
        private GameFITClub game;
        private Matrix[] transforms;

        public BackgroundItemConfig Config { get; private set; }

        public Model ItemModel { get; private set; }

        public BackgroundItem(GameFITClub game, BackgroundItemConfig config)
            : base(game)
        {
            this.Config = config;
            this.game = game;
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            this.ReloadConfig();
        }

        public void ReloadConfig()
        {
            UpdateWorld();
            if (this.Config.Alpha == 0)
            {
                this.Config.Alpha = 1;
            }
            try
            {
                this.ItemModel = this.Game.Content.Load<Model>(this.Config.ModelPath);
                this.transforms = new Matrix[this.ItemModel.Bones.Count];
                this.ItemModel.CopyAbsoluteBoneTransformsTo(this.transforms);
            }
            catch (Exception exc)
            {
                Console.Error.WriteLine(exc);
            }
        }

        public void UpdateWorld()
        {
            this.worldMatrix = this.Config.Transforms.CreateTransform();
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (this.ItemModel != null)
            {
                foreach (ModelMesh mesh in ItemModel.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.Alpha = 1.0f;
                        effect.LightingEnabled = true;  //zapnout osvetlovaci system
                        if (this.Config.NotTextured)
                        {
                            effect.EnableDefaultLighting();
                            effect.AmbientLightColor = new Vector3(0.6f, 0.6f, 0.8f);
                            effect.PreferPerPixelLighting = true;
                            effect.TextureEnabled = false;
                        }
                        else
                        {
                            effect.TextureEnabled = true;
                            //effect.EnableDefaultLighting();
                            //effect.PreferPerPixelLighting = true;
#if WINDOWS_PHONE

                            effect.DirectionalLight0.DiffuseColor = new Vector3(0.1f, 0.1f, 0.1f);
#else
                            //nastaveni a zapnuti jednoho z XNA defaultnich svetel
                            effect.DirectionalLight0.DiffuseColor = Vector3.One; // bile svetlo
#endif
                            effect.DirectionalLight0.Direction = new Vector3(0, -3, -5);  //osvetleni prichazi z +Y osy

                            //nastaveni osvetlenych ploch
                            effect.DiffuseColor = Vector3.One;              //de facto barva osvetlenych ploch
                            //effect.EmissiveColor = new Vector3(0.3f, 0, 0);           //emisni slozka, celkem k nicemu
                            effect.SpecularColor = new Vector3(0.2f, 0.2f, 0.2f);       //slozka odrazu svetla, celkem k nicemu u jednoduchych objektu, celkem fajn u slozitych

                            //nastaveni osvetlenych ploch
                            effect.AmbientLightColor = new Vector3(0.75f, 0.75f, 0.75f);   // de facto barva mist na predmetu zastinenych tim samym predmetem
                        }
                        effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;
                        effect.View = game.ViewMatrix;
                        effect.Projection = game.ProjectionMatrix;
                    }
                    mesh.Draw();
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}